package com.example.practice4;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.security.Provider;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    public void onClick(View view) {
        Integer a;
        switch (view.getId()){
            case R.id.Start:
                Intent S = new Intent(MainActivity.this,MyService.class);
                bindService(S,myConnection, Service.BIND_AUTO_CREATE);
                Toast.makeText(this,"服务已启动!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.Stop:
                if(myservice!=null){
                    unbindService(myConnection);
                    Toast.makeText(this,"服务已注销!",Toast.LENGTH_SHORT).show();
                    Show.setText("");
                }else
                    Toast.makeText(this,"服务尚未开始!",Toast.LENGTH_SHORT).show();
                break;
            case R.id.add:
                if(myservice!=null){
                     a =myservice.add(10,2);
                    Show.setText(a.toString());
                }
                break;
            case R.id.sub:
                if(myservice!=null){
                    a =myservice.sub(10,2);
                    Show.setText(a.toString());
                }
                break;
            case R.id.times:
                if(myservice!=null){
                    a =myservice.Times(
                            10,2);
                    Show.setText(a.toString());
                }
                break;
            case R.id.div:
                if(myservice!=null){
                    Double b=myservice.div(10,2);
                    Show.setText(b.toString());
                }
                break;
        }
    }

    private MyService myservice;
    private ServiceConnection myConnection;
    private Button add,sub,div,times,start,stop;
    private TextView Show;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Show=findViewById(R.id.show);
        start=findViewById(R.id.Start);
        stop=findViewById(R.id.Stop);
        add=findViewById(R.id.add);
        sub=findViewById(R.id.sub);
        times=findViewById(R.id.times);
        div=findViewById(R.id.div);
        start.setOnClickListener(this);
        stop.setOnClickListener(this);
        add.setOnClickListener(this);
        sub.setOnClickListener(this);
        div.setOnClickListener(this);
        times.setOnClickListener(this);
        myConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                myservice=((MyService.LocalBinder)iBinder).getService();
            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        };

    }
}